package com.starnet.studentmanagement.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.starnet.studentmanagement.entity.CourseSelection;
import com.starnet.studentmanagement.entity.Students;
import com.starnet.studentmanagement.handler.ApiException;
import com.starnet.studentmanagement.mapper.CourseSelectionMapper;
import com.starnet.studentmanagement.mapper.StudentsMapper;
import com.starnet.studentmanagement.service.StudentsService;
import com.starnet.studentmanagement.util.PageUtil;
import com.starnet.studentmanagement.vo.PageResult;
import com.starnet.studentmanagement.vo.PageVO;
import com.starnet.studentmanagement.vo.Result;
import com.starnet.studentmanagement.vo.ResultCode;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sangeng
 * @since 2022-03-26
 */
@Service
public class StudentsServiceImpl implements StudentsService {
    @Autowired
    StudentsMapper studentsMapper;
    @Autowired
    CourseSelectionMapper courseSelectionMapper;


    @Override
    public PageResult searchByName(String sname, Integer page, Integer pageSize) {
        LambdaQueryWrapper<Students> queryWrapper = new LambdaQueryWrapper<>();
        //有传学生名字 就用名字模糊查询 否则默认查询全部
        if (sname != null) {
            queryWrapper.like(Students::getSname, sname);
        }
        IPage<Students> iPage = new Page<>();
        iPage.setCurrent(page);
        //如果有传入页面大小 设置分页大小 否则用默认
        if (pageSize != null) {
            iPage.setSize(pageSize);
        }
        studentsMapper.selectPage(iPage, queryWrapper);
        if (iPage.getTotal() == 0) {
            throw new ApiException(ResultCode.EMPTY_RESULT.getCode(),ResultCode.EMPTY_RESULT.getMsg());
        }
        PageVO pageVO = PageUtil.getPageVO(iPage);
        return PageResult.success(iPage.getRecords(), pageVO);
    }

    @Override
    public Result addNewStudent(Students student) {
        LambdaQueryWrapper<Students> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Students::getSid, student.getSid());
        Students checkStudent = studentsMapper.selectOne(queryWrapper);
        if (checkStudent != null) {
            throw new ApiException(ResultCode.EXISTED.getCode(),"当前学号已被占用");
        }
        int insert = studentsMapper.insert(student);
        if (insert == 0) {
            throw  new ApiException(ResultCode.UNKNOWN_ERROR.getCode(),"插入失败，服务器未知错误");
        }
        return Result.success(null);
    }

    @Override
    public Result updateStudent(Students student) {
        LambdaQueryWrapper<Students> queryWrapper= new LambdaQueryWrapper();
        queryWrapper.eq(Students::getSid,student.getSid());
        queryWrapper.ne(Students::getId,student.getId());
        Students students = studentsMapper.selectOne(queryWrapper);
        if (students!=null){
            throw new ApiException(ResultCode.EXISTED.getCode(), "此学号已被占用");
        }
        LambdaUpdateWrapper<Students> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Students::getId, student.getId());
        int update = studentsMapper.update(student, updateWrapper);
        if (update == 0) {
           throw new ApiException(ResultCode.NOT_FOUND.getCode(),"更新失败，该学生不存在或已被删除");
        }
        return Result.success(null);
    }
    @Transactional
    @Override
    public Result deleteById(int id) {
        Students students = studentsMapper.selectById(id);
        if (students == null) {
            throw new ApiException(ResultCode.NOT_FOUND.getCode(), "该学生不存在或已被删除");
        }
        LambdaQueryWrapper<CourseSelection> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseSelection::getSid,students.getSid());
        studentsMapper.deleteById(id);
        courseSelectionMapper.delete(queryWrapper);

        return Result.success(null);
    }


}
